﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ISS.CaseStudy.BLL;
using ISS.CaseStudy.Entities;

namespace CaseStudy.AdminPortal
{
    public partial class CategoryInfoDetail : System.Web.UI.Page
    {
        private bool isEdit = true;
        private string id = string.Empty;
        private string categoryType = string.Empty;
        private string mode = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            loginInfo.Login();

            string cn_CategoryType = string.Empty;

            if (Request.QueryString["type"] != null)
            {
                categoryType = Request.QueryString["type"];

                if (!IsPostBack)
                {
                    parentLink.NavigateUrl = "CategoryManager.aspx?type=" + categoryType;

                    if (categoryType.Equals("Technology"))
                    {
                        lblCategoryTitle.Text = "技术类型信息：";
                        lblTitle.Text = "技术类型：";
                        parentLink.Text = "技术类型信息管理";
                        cn_CategoryType = "技术类型";
                    }
                    else if (categoryType.Equals("Industry"))
                    {
                        lblCategoryTitle.Text = "行业类型信息：";
                        lblTitle.Text = "行业类型：";
                        parentLink.Text = "行业类型信息管理";
                        cn_CategoryType = "行业类型";
                    }
                    else if (categoryType.Equals("Client"))
                    {
                        lblCategoryTitle.Text = "客户名称信息：";
                        lblTitle.Text = "客户名称：";
                        parentLink.Text = "客户名称信息管理";
                        cn_CategoryType = "客户名称";
                    }
                    else
                    {
                        lblCategoryTitle.Text = "服务类型信息：";
                        lblTitle.Text = "服务类型：";
                        parentLink.Text = "服务类型信息管理";
                        cn_CategoryType = "服务类型";
                    }
                }
            }

            if (Request.QueryString["mode"] != null)
                mode = Request.QueryString["mode"].ToLower();



            if (Request.QueryString["id"] != null)
            {
                id = Request.QueryString["id"];

                if (!IsPostBack)
                {
                    BindData();
                }

                if (mode.Equals("edit"))
                {
                    isEdit = true;
                    currentMapItem.InnerText = "编辑" + cn_CategoryType;
                }
                else
                {
                    isEdit = false;
                    currentMapItem.InnerText = "查看" + cn_CategoryType;

                    txtCategoryValue.Enabled = false;
                    imgbtnConfirm.Visible = false;
                    imgbtnReset.Visible = false;
                }
            }
            else
            {
                isEdit = false;
                currentMapItem.InnerText = "新建" + cn_CategoryType;
            }
        }

        protected void imgbtnConfirm_Click(object sender, ImageClickEventArgs e)
        {
            if (!ValidateDates())
            {
                return;
            }

            string categoryValue = HttpUtility.HtmlEncode(txtCategoryValue.Text);
            string message = string.Empty;

            if (categoryType.Equals("Technology"))
            {
                TechnologyInfo technologyInfo = new TechnologyInfo();
                technologyInfo.Value = categoryValue;
                technologyInfo.ModifyTime = DateTime.Now;


                if (!isEdit)
                {
                    technologyInfo.CreateTime = DateTime.Now;
                    if (!TechnologyBLL.Insert(technologyInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "创建技术类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
                else
                {
                    technologyInfo.Id = id;
                    if (!TechnologyBLL.Update(technologyInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "修改技术类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
            }
            else if (categoryType.Equals("Industry"))
            {
                IndustryInfo industryInfo = new IndustryInfo();
                industryInfo.Value = categoryValue;
                industryInfo.ModifyTime = DateTime.Now;

                if (!isEdit)
                {
                    industryInfo.CreateTime = DateTime.Now;
                    if (!IndustryBLL.Insert(industryInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "创建行业类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
                else
                {
                    industryInfo.Id = id;
                    if (!IndustryBLL.Update(industryInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "修改行业类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
            }
            else if (categoryType.Equals("Client"))
            {
                ClientInfo clientInfo = new ClientInfo();
                clientInfo.Value = categoryValue;
                clientInfo.ModifyTime = DateTime.Now;

                if (!isEdit)
                {
                    clientInfo.CreateTime = DateTime.Now;
                    if (!ClientBLL.Insert(clientInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "创建客户类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
                else
                {
                    clientInfo.Id = id;
                    if (!ClientBLL.Update(clientInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "修改客户类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
            }
            else
            {
                ServiceInfo serviceInfo = new ServiceInfo();
                serviceInfo.Value = categoryValue;
                serviceInfo.ModifyTime = DateTime.Now;

                if (!isEdit)
                {
                    serviceInfo.CreateTime = DateTime.Now;
                    if (!ServiceBLL.Insert(serviceInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "创建服务类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
                else
                {
                    serviceInfo.Id = id;
                    if (!ServiceBLL.Update(serviceInfo, out message))
                    {
                        divReminding.InnerHtml = message;
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Red");
                        return;
                    }
                    else
                    {
                        divReminding.InnerHtml = "修改服务类型信息成功！";
                        divReminding.Visible = true;
                        divReminding.Style.Add("color", "Green");
                        return;
                    }
                }
            }

            //Response.Redirect("CategoryManager.aspx?type=" + categoryType.ToLower());
        }

        protected void imgbtnReset_Click(object sender, ImageClickEventArgs e)
        {
            divReminding.InnerHtml = "";
            divReminding.Visible = false;

            if (!isEdit)
            {
                txtCategoryValue.Text = string.Empty;
            }
            else
            {
                BindData();
            }
        }

        protected void imgBtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("CategoryManager.aspx?type=" + categoryType);
        }

        private void BindData()
        {
            if (categoryType.Equals("Technology"))
            {
                TechnologyInfo tempInfo = TechnologyBLL.GetInfo(id);
                if (tempInfo != null)
                {
                    txtCategoryValue.Text = HttpUtility.HtmlDecode(tempInfo.Value);
                }
            }
            else if (categoryType.Equals("Industry"))
            {
                IndustryInfo tempInfo = IndustryBLL.GetInfo(id);
                if (tempInfo != null)
                {
                    txtCategoryValue.Text = HttpUtility.HtmlDecode(tempInfo.Value);
                }
            }
            else if (categoryType.Equals("Client"))
            {
                ClientInfo tempInfo = ClientBLL.GetInfo(id);
                if (tempInfo != null)
                {
                    txtCategoryValue.Text = HttpUtility.HtmlDecode(tempInfo.Value);
                }
            }
            else
            {
                ServiceInfo tempInfo = ServiceBLL.GetInfo(id);
                if (tempInfo != null)
                {
                    txtCategoryValue.Text = HttpUtility.HtmlDecode(tempInfo.Value);
                }
            }
        }

        private bool ValidateDates()
        {
            if (string.IsNullOrEmpty(txtCategoryValue.Text))
            {
                if (categoryType.Equals("Technology"))
                {
                    divReminding.InnerHtml = "技术名称不能为空！";
                }
                else if (categoryType.Equals("Industry"))
                {
                    divReminding.InnerHtml = "行业名称不能为空！";
                }
                else if (categoryType.Equals("Client"))
                {
                    divReminding.InnerHtml = "客户名称不能为空！";
                }
                else
                {
                    divReminding.InnerHtml = "服务类型名称不能为空！";
                }

                divReminding.Style.Add("color", "Red");
                divReminding.Visible = true;
                return false;
            }

            if (HttpUtility.HtmlEncode(txtCategoryValue.Text).Length > 49)
            {
                if (categoryType.Equals("Technology"))
                {
                    divReminding.InnerHtml = "技术名称太长了！";
                }
                else if (categoryType.Equals("Industry"))
                {
                    divReminding.InnerHtml = "行业名称太长了！";
                }
                else if (categoryType.Equals("Client"))
                {
                    divReminding.InnerHtml = "客户名称太长了！";
                }
                else
                {
                    divReminding.InnerHtml = "服务类型名称太长了！";
                }

                divReminding.Style.Add("color", "Red");
                divReminding.Visible = true;
                return false;
            }

            return true;
        }
    }
}
